

include config.mak

OBJECTS=sdk_llist.o sdk_mem.o sdk_util.o sdk_solv.o sdk_tree.o
TEST_PROGS=sdk_data_sizes sdk_llist_test sdk_test

INCLUDE=-I. -I../lib/include
LIBS=-L../lib -lmatvec
SUDOKU=-L. -lsudoku

### Main targets

all: sdk-info sudoku sdk-gen test 
	cp $(TEST_PROGS) ../bin
	if [ ! -e ../bin/sudosolve ]; then \
		cd ../bin && ln -s sudosolve.$(VERSION) sudosolve ; \
	fi
	cp -f libsudoku.so.$(VERSION) ../bin
	if [ ! -e ../bin/sdk_gen ]; then \
		cd ../bin && ln -s sdk_gen.$(VERSION) sdk_gen ; \
	fi
	cd ../bin && ldconfig -v -n .

sudoku: libsudoku.so 
	gcc -o sudosolve.$(VERSION) -I. -I../matvec/src \
		-lc -lm $(SUDOKU) $(LIBS) sudosolve.c
	cp sudosolve.$(VERSION) ../bin

sdk-gen: libsudoku.so
	gcc -o sdk_gen.$(VERSION) -I. -I../matvec/src \
		-lc -lm $(SUDOKU) $(LIBS) sdk_gen.c
	cp sdk_gen.$(VERSION) ../bin

libsudoku.so: $(OBJECTS)
	ld -shared -soname libsudoku.so -o libsudoku.so.$(VERSION) \
		$(OBJECTS) -lc -lm $(LIBS)
	ldconfig -v -n .

test: $(TEST_PROGS)

### Install targets

install:
	cp sudosolve.$(VERSION) /usr/local/bin/sudosolve
	chmod 755 /usr/local/bin/sudosolve
	cp libsudoku.so* /usr/local/lib

sdk-info:
	@echo "+----------------------------------------------------------+"
	@echo "Building sudosolve version $(VERSION)"
	@echo "  CC:           $(CC)"
	@echo "  C_FLAGS:      $(C_FLAGS)"
	@echo "  C_TEST_FLAGS: $(C_TEST_FLAGS)"
	@echo "  CPP_FLAGS:    $(CPP_FLAGS)"
	@echo "  OBJECTS:      $(OBJECTS)"
	@echo "  TEST_PROGS:   $(TEST_PROGS)"
	@echo "  INCLUDE:      $(INCLUDE)"
	@echo "  LIBS:         $(LIBS)"

### Source building targets

.c: libsudoku.so
	$(CC) $(C_TEST_FLAGS) -o $@ $< $(CPP_FLAGS) $(INCLUDE) \
		-lc -lm $(LIBS) $(SUDOKU)

.c.o:
	$(CC) $(C_FLAGS) $(CPP_FLAGS) $< $(INCLUDE)


### Cleaning targets

dist-clean: clean
	rm -f config.mak
	rm -f config.h

clean:
	rm -f *.o
	rm -f sudosolve
	rm -f sudosolve.$(VERSION)
	rm -f $(TEST_PROGS)
	rm -f libsudoku.so*
	rm -f sdk_gen.$(VERSION)
	rm -f ../bin/sudosolve*
	rm -f ../bin/sdk_gen*
	rm -f ../bin/sdk_list_test ../bin/sdk_test
	rm -f ../bin/libsudoku*
